export class MyTimerUtils {
  static build(callback: (i: number) => void, interval?: number) {
    return new MyTimerUtils(callback, interval)
  }
  private timer: number | undefined = undefined
  private callback: (i: number) => void
  private interval: number = 500
  private count: number = 0
  // 构造函数接收回调函数和可选的时间间隔
  private constructor(callback: (i: number) => void, interval?: number) {
    this.callback = callback
    if (interval) this.interval = interval
  }

  // 开始计时器
  public start() {
    if (this.timer) {
      this.stop()
    }
    // //立即执行一次
    // this.callback(this.count)
    //定时器开启
    this.timer = setInterval(() => {
      this.count++
      this.callback(this.count)
    }, this.interval)

    // log('计时器开始')
  }

  // 停止计时器
  public stop() {
    if (this.timer) {
      clearInterval(this.timer)
      this.timer = undefined
    }
    this.count = 0
    // log('计时器已停止')
  }
  // 获取计时器状态
  public isRunning() {
    return this.timer
  }
}
